package org.example.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FilterApple3 {
    //策略模式
    @FunctionalInterface
    public interface AppleFilter {
        boolean filter(long w);
    }

    public static List<Apple> findApple(List<Apple> apples, AppleFilter appleFilter) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            //apple.getWeight()计算后作为入参
            if (appleFilter.filter(apple.getWeight()))
                list.add(apple);
        }
        return list;
    }

    public static class GreenAnd160WeightFilter implements AppleFilter {
        @Override
        public boolean filter(long w) {
            return w >= 160;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 170), new Apple("green", 200));
        List<Apple> result = findApple(list, new GreenAnd160WeightFilter());
        System.out.println(result);

        //匿名类写法
        List<Apple> apples = findApple(list, new AppleFilter() {
            @Override
            public boolean filter(long w) {
                return w > 160;
            }
        });
        System.out.println(apples);

        //Lambda表达式
        List<Apple> lambdaResult = findApple(list, w -> w > 160);
        System.out.println(lambdaResult);
    }
}
